package JAVA300.第十一章多线程;

/**
 * 龟兔赛跑
 * 谁先走够100步谁就获胜
 */

public class thread_06_webRacer implements Runnable{

    private  String winner;

    @Override
    public void run() {
        for (int steps= 1;steps<=100;steps++){
            //模拟兔子休息
            if (Thread.currentThread().getName().equals("rabbit") && steps%10 ==0){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            //输出比赛进程 各走各的
            System.out.println(Thread.currentThread().getName()+"--->"+steps);
            //每走一步看比赛是否结束
            boolean flag =getWinner(steps);
            if (flag){
                break;
            }
        }

    }


    //判断比赛结束了
    private boolean getWinner(int steps){
        //首先判断有没有胜利者

        if (winner != null){
            return true;
        }else {

            if (steps == 100){

                winner = Thread.currentThread().getName();
                System.out.println("胜者是："+winner);
                return true;
            }

        }
        return false;




    }

    public static void main(String[] args) {
        thread_06_webRacer racer = new thread_06_webRacer();
        new Thread(racer,"tortoise").start();
        new Thread(racer,"rabbit").start();
    }
}
